var AV = require('leanengine');
var async = require('async');
var promise = new AV.Promise();
var Address = AV.Object.extend("UserAddress");
var query = new AV.Query(Address);
module.exports = {
    //获取用户地址列表
    listAddress:function(uid){
        query = new AV.Query(Address);
        if(uid){
            query.equalTo('uid',uid);
            return query.find();
        } else{
            return new AV.Promise.error({code:101});
        }
    },
    /**
     * 通过用户地址编号获取用户地址信息
     * @param objectId　用户地址id
     * @auth James 2015-09-07
     */
    getAddressByObjId:function(objectId){
        var query = new AV.Query(Address);
        if(objectId){
            return query.get(objectId);
        } else{
            return new AV.Promise.error({code:149});
        }
    },
    //获得指定地址
    getEditAddress:function(objectId){
        if(userid){
            query = new AV.Query(Address);
            return query.get(objectId);
        } else{
            return new AV.Promise.error({code:101});
        }
    },
    //获取用户的默认地址
    getDefaultAddress:function(userid){
        query = new AV.Query(Address);
        if(userid){
            query.equalTo('uid',userid);
            query.equalTo('isDefault',true);
            return query.first();
        } else{
            //throw 'userid should not be empty!';
            return new AV.Promise.error({code:101});
        }
    },
    //设置用户默认地址(将原默认地址还原，并设置新默认地址，然后返回所有地址列表)
    setDefaultAddress:function(objectId,uid){
        if(objectId&&uid){
            query = new AV.Query(Address);
            var promise = new AV.Promise();
             async.waterfall([
                function(callback){
                    query.equalTo('uid',uid);
                    query.equalTo('isDefault',true);
                    query.first().then(function(res){
                            callback(null,res);
                    })
                },
                function(res,callback){
                    if(res){
                        res.set('isDefault',false);
                        res.save().then(function(res){
                            callback(null,res,objectId);
                        });
                    }else{
                        callback(null,res,objectId);
                    }
                },
                function(res,objectId,callback){
                    query = new AV.Query(Address);
                    query.get(objectId).then(function(res){
                        res.set('isDefault',true);
                        res.save().then(function(res){
                            callback(null,res);
                        });
                    }).catch(function(err){
                        callback(err);
                    });
                },
                 function (res,callback) {
                     query = new AV.Query(Address);
                     query.equalTo('uid',uid);
                     query.find().then(function(res){
                         callback(null,res);
                     });
                 },
            ],function(err,result){
                if(err){
                    promise.reject({code:103});
                }else{
                    promise.resolve(result);
                }
            });
            return promise;
        }else{
            if(!uid){
                return new AV.Promise.error({code:101});
            }else{
                return new AV.Promise.error({code:102});
            }
            //throw 'objectId should not be empty!';

        }
    },
    //创建新的用户地址
    saveAddress:function(userAddress){
        var addr = new Address();
        var promise = new AV.Promise();
        addr.save(userAddress).then(function(res){
            promise.resolve(res);
        });
        return promise;
    },
    //删除用户地址
    deleteAddress:function(objectId){
        if(objectId){
            var promise = new AV.Promise();
                query = new AV.Query(Address);
                async.waterfall([
                     function(callback){
                         query.get(objectId).then(function(res){
                                callback(null,res);
                         }).catch(function(err){
                             callback(err);
                         });
                    },
                    function(res,callback){
                        res.destroy().then(function(){
                            callback(null,{code:0,msg:'删除成功',res:res});
                        });
                    }
                ],function(err,result){
                    if(err){
                        promise.reject({code:104});
                    }else{
                        promise.resolve(result);
                    }

                });
                return promise;
            }else{
                return new AV.Promise.error({code:102});
            }
        }
};


